﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace VoltGameEditor
{
	public partial class FIRST_RUN : Form
	{
		int currTabIndex = 0;
		public FIRST_RUN()
		{
			InitializeComponent();
		}

		private int GetTabIndex(Point p)
		{
			int result = -1;
			for (int i1 = 0; i1 < tabControl1.TabCount; i1++)
			{
				if (tabControl1.GetTabRect(i1).Contains(p))
					return i1;
			}
			return result;
		}

		private void FIRST_RUN_FormClosing(object sender, FormClosingEventArgs e)
		{

		}

		private void button3_Click(object sender, EventArgs e)
		{
			//SaveSettings here..
			//not finished
			this.Close();
		}

		private void tabControl1_MouseClick(object sender, MouseEventArgs e)
		{
			currTabIndex = GetTabIndex(e.Location);
		}

		private void b_prevPage_Click(object sender, EventArgs e)
		{
			if (currTabIndex != 0)
			{ tabControl1.SelectedIndex--; currTabIndex--; }
		}

		private void b_nextPage_Click(object sender, EventArgs e)
		{
			if (currTabIndex < tabControl1.TabPages.Count - 1)
			{ tabControl1.SelectedIndex++; currTabIndex++; }
		}

		private void FIRST_RUN_Load(object sender, EventArgs e)
		{
			tree_dock.Nodes.Add("Object Browser");
			tree_dock.Nodes[0].Tag = DockState.DockRight;
			tree_dock.Nodes.Add("Material Browser");
			tree_dock.Nodes[1].Tag = DockState.DockLeftAutoHide;
			tree_dock.Nodes.Add("Object Factory");
			tree_dock.Nodes[2].Tag = DockState.DockLeftAutoHide;
			tree_dock.Nodes.Add("Prefab Factory");
			tree_dock.Nodes[3].Tag = DockState.DockLeftAutoHide;
			tree_dock.Nodes.Add("Terrain Tools");
			tree_dock.Nodes[4].Tag = DockState.DockLeftAutoHide;

			foreach (string dir in System.IO.Directory.GetDirectories("Games\\"))
			{
				tree_games.Nodes.Add(dir.Remove(0,dir.IndexOf("\\")+1));
				//add game specific code here...
			}
		}

		private void tree_dock_AfterSelect(object sender, TreeViewEventArgs e)
		{
			switch ((DockState)e.Node.Tag)
			{
				case DockState.DockLeft:
					radioButton1.Checked = true;
					break;
				case DockState.DockLeftAutoHide:
					radioButton2.Checked = true;
					break;
				case DockState.DockRight:
					radioButton3.Checked = true;
					break;
				case DockState.DockRightAutoHide:
					radioButton4.Checked = true;
					break;
			}
		}

		private void radioButton1_CheckedChanged(object sender, EventArgs e)
		{
			if (tree_dock.SelectedNode != null)
				tree_dock.SelectedNode.Tag = DockState.DockLeft;
		}

		private void radioButton2_CheckedChanged(object sender, EventArgs e)
		{
			if (tree_dock.SelectedNode != null)
				tree_dock.SelectedNode.Tag = DockState.DockLeftAutoHide;
		}

		private void radioButton3_CheckedChanged(object sender, EventArgs e)
		{
			if (tree_dock.SelectedNode != null)
				tree_dock.SelectedNode.Tag = DockState.DockRight;
		}

		private void radioButton4_CheckedChanged(object sender, EventArgs e)
		{
			if (tree_dock.SelectedNode != null)
				tree_dock.SelectedNode.Tag = DockState.DockRightAutoHide;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			Folders.ShowDialog();
			if (Folders.SelectedPath!="")
				tb_gameDir.Text = Folders.SelectedPath;
		}

		private void b_finish_Click(object sender, EventArgs e)
		{
			//Write Settings, then close the dialog...
			AppSettings.Gamemode.GameExecutable = tb_gameDir.Text;

			AppSettings.MapView.DockedWindowPositions["ObjPane"] = (DockState)tree_dock.Nodes[0].Tag;
			AppSettings.MapView.DockedWindowPositions["MatBrowser"] = (DockState)tree_dock.Nodes[1].Tag;
			AppSettings.MapView.DockedWindowPositions["TerrTools"] = (DockState)tree_dock.Nodes[2].Tag;
			AppSettings.MapView.DockedWindowPositions["Prefab Factory"] = (DockState)tree_dock.Nodes[3].Tag;
			AppSettings.MapView.DockedWindowPositions["CustObj"] = (DockState)tree_dock.Nodes[4].Tag;

			if (rb_highlightOn.Checked)
				AppSettings.MapView.HighlightObectsOnHover = true;
			else
				AppSettings.MapView.HighlightObectsOnHover = false;
			if (rb_rulersOn.Checked)
				AppSettings.MapView.SelectedObjectRulers = true;
			else
				AppSettings.MapView.SelectedObjectRulers = false;


			this.Close();
		}


	}
}
